# <license>
# This software is distributed under the Simplified BSD License:
#
# Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice, 
#     	this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright notice, 
#     	this list of conditions and the following disclaimer in the documentation 
#     	and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
# SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
# DAMAGE.
# </license>
MAKEDEPTH ?= 0
MAKEDEPTH := $(shell echo $$[ $(MAKEDEPTH)+1 ])
$(info $ $(MAKEDEPTH) + Makefile)

all:

# override the GENERIK_PATH variable with the current directory
#GENERIK_SOURCE_PATH ?= $(shell pwd)
GENERIK_SOURCE_PATH ?= $(abspath .)
GENERIK_PATH = $(GENERIK_SOURCE_PATH)
$(info -- overriding GENERIK_PATH with pwd: $(GENERIK_PATH))

# load the installation Makefile rules
include $(GENERIK_PATH)/Makefiles/Makefile.install

MINOR = 1
MAJOR = 0
REVISION = 0
VERSION = $(MAJOR).$(MINOR).$(REVISION)
LIBNAME = generiktracing

BUILD = DEBUG

BINPOSTFIX = bin
LIBPOSTFIX = lib
INCPOSTFIX = include/generik
PKGPOSTFIX = lib/pkgconfig

# we would like to use this directory, and not the (possibly) installed generik package!
DISABLE_GENERIK_PKG = 1
# make sure the output is verbose, so that we can have a good idea of what goes on/wrong
GENERIK_MAKE_VERBOSE = 1

# if GENERIK_SILENCE is defined, pass it along as a C/C++ flag
# This in effect builds a mute tracing library
# by combining this with BUILD=RELASE, we get simulate not linking with generik::tracing at all
ifdef GENERIK_SILENCE
CFLAGS += -DGENERIK_SILENCE
BUILD = RELEASE
endif

EXCLUDEDIRS = Makefiles database dbspi gtk opengl tests examples

CFLAGS += -I.

LDLIBS += -lpthread

#include $(GENERIK_PATH)/Makefiles/Makefile.env

LIBTRACING  = lib$(LIBNAME).$(SOLIB_EXT)
$(LIBTRACING): LINK.so.FLAGS += -Wl,-soname,$(LIBTRACING).$(MAJOR)
$(LIBTRACING): tracing.o

TARGETS += $(LIBTRACING)
LINKCC += $(LIBTRACING)

install: install_bin install_includes install_libs install_pkg post_install

uninstall: uninstall_bin uninstall_libs uninstall_pkg uninstall_includes post_uninstall

BINSOURCES = setgenerik.sh
LIBSOURCES = $(LIBTRACING) 
INCSOURCES = *.h database dbspi gtk opengl
PKGSOURCES = generik.pc
EXTSOURCES = Makefile.generik testing.py Makefiles

#VERBOSE_VALUES += BINSOURCES LIBSOURCES INCSOURCES PKGSOURCES EXTSOURCES

BINDIR = $(abspath $(PREFIX)/$(BINPOSTFIX))
PKGDIR = $(abspath $(PREFIX)/$(PKGPOSTFIX))
LIBDIR = $(abspath $(PREFIX)/$(LIBPOSTFIX))
INCDIR = $(abspath $(PREFIX)/$(INCPOSTFIX))

#VERBOSE_VALUES += BINDIR LIBDIR INCDIR PKGDIR

install_bin:
	@echo "-- installing binaries"
	$(MKDIR) $(BINDIR)
	$(MKINSTALL) $(BINSOURCES) $(BINDIR)
	@echo "-- setting the PKGDIR placeholder to $(PKGDIR) in $(BINDIR)/setgenerik.sh"
	awk '{gsub("PKGDIR", "$(PKGDIR)"); print > FILENAME}' $(BINDIR)/setgenerik.sh

install_libs: $(LIBSOURCES)
	@echo "-- installing libraries"
	$(MKDIR) $(LIBDIR)
	$(MKINSTALL) $(LIBSOURCES) $(LIBDIR)
ifdef USING_LINUX # have to rename libNAME.so to libNAME.so.x.y, remember to run ldconfig to create libNAME.so.x
	@echo "-- creating library shortcuts"
	mv $(LIBDIR)/$(LIBTRACING) $(LIBDIR)/$(LIBTRACING).$(VERSION)
	ln -s $(LIBDIR)/$(LIBTRACING).$(VERSION) $(LIBDIR)/$(LIBTRACING)
	@echo "NOTE: please remember to run ldconfig to update the link to $(LIBDIR)/$(LIBTRACING).$(VERSION)"
endif
	@echo "-- installing exts"
	$(MKDIR) $(LIBDIR)/generik
	$(MKINSTALL) $(EXTSOURCES) $(LIBDIR)/generik

install_pkg:
	@echo "-- installing pkgconfig files"
	$(MKDIR) $(PKGDIR)
	$(MKINSTALL) $(PKGSOURCES) $(PKGDIR)
	@echo "-- setting the PREFIX placeholder to $(PREFIX) in $(PKGDIR)/generik.pc"
	awk '{gsub("PREFIX", "$(PREFIX)"); print > FILENAME}' $(PKGDIR)/generik.pc

install_includes:
	@echo "-- installing include files"
	$(MKDIR) $(INCDIR)
	$(MKINSTALL) $(INCSOURCES) $(INCDIR)

uninstall_bin:
	@echo "-- uninstalling binaries"
	cd $(BINDIR) && rm -rf $(BINSOURCES)

uninstall_libs:
	@echo "-- uninstalling libraries"
	cd $(LIBDIR) && rm -rf $(LIBSOURCES)
ifdef USING_LINUX
	cd $(LIBDIR) && rm -rf $(LIBTRACING).$(VERSION) $(LIBTRACING).$(MAJOR)
endif
	rm -rf $(LIBDIR)/generik

uninstall_pkg:
	@echo "-- uninstalling pkgconfig files"
	cd $(PKGDIR) && rm -rf $(PKGSOURCES)

uninstall_includes:
	@echo "-- uninstalling include files"
	rm -rf $(INCDIR)

post_install:
	@echo "NOTE: generik has been installed in $(PREFIX)"
	@echo "NOTE: The subdirectory $(BIN_PREFIX) needs to be added to your PATH in order"
	@echo "NOTE: for the system to find generik scripts and executables."
	@echo "NOTE: The subdirectory $(LIBDIR) needs to be added to either your ld.so.conf"
	@echo "NOTE: path, or directly into your LD_LIBRARY_PATH variable, so that generik"
	@echo "NOTE: shared libraries can be found by any executables which need them."

post_uninstall:
	@echo "NOTE: generik has been uninstalled from $(PREFIX)"
	@echo "NOTE: The subdirectory $(BIN_PREFIX) may still be in your PATH, and you might"
	@echo "NOTE: want to remove it, if that PATH is not used by other components in the system."
	@echo "NOTE: The subdorectory $(LIB_PREFIX) may still be in your ld.so.conf path, or"
	@echo "NOTE: directly in your LD_LIBRARY_PATH variable, and you may want to remove it,"
	@echo "NOTE: if it is not required by other components of the system."

include $(GENERIK_PATH)/Makefile.generik

$(info $ $(MAKEDEPTH) - Makefile)
MAKEDEPTH := $(shell echo $$[ $(MAKEDEPTH)-1 ])
# END





